package com.gaojiawei.multiThread18_CompletableFutureTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 任务A 和 任务B 并行执行，任务A和任务B只要有一个执行完了，执行任务C
 */
public class L07_ThenApplyToEitherThenAcceptEitherRunAfterBoth {

    public static void main(String[] args) {
        testThenApplyToEither();
    }

    public static void testThenApplyToEither(){
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);


        CompletableFuture<String> taskC = CompletableFuture.supplyAsync(()->{
            System.out.println("begin a");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("finish a");
            return "a task is ok";
        }).applyToEither(CompletableFuture.supplyAsync(()->{
            System.out.println("begin b");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("begin b");
            return "b task is ok";
        }),resFirst->{
            System.out.println("begin c");

            System.out.println("finish c");

            return "fisrt"+resFirst;
        });

        System.out.println(taskC.join());
    }
}
